package org.gdkm.el;

import java.io.File;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 数据服务类. </br> 用于减低的程序的耦合性. </br> 日期: 2012-10-13
 * <p>
 * 
 * @author Arylo<<b>arylo.open@gmail.com</b>>
 * @version 1.0
 */
public class DataBaseServer {
	private static Document doc = null;
	private static Element root = null;

	/**
	 * 构建一个DataBaseServer对象.
	 */
	public DataBaseServer() {
		if (Intent.getDataBasePath() != null) {
			try {
				doc = new SAXReader().read(new File(Intent.getDataBasePath()));
			} catch (Exception e) {
				e.printStackTrace();
			}
			root = doc.getRootElement();
		}
		Intent.writeLog("Server", "DataBaseServer Init Finished.");
	}

	/**
	 * 检查账号密码是否正确.
	 * 
	 * @param id
	 *            账号
	 * @param pwd
	 *            密码
	 * @return 正确就返回账号名,否则返回null.
	 */
	public static String checkAccout(String id, String pwd) {
		for (Object _o : root.elements("Usr")) {
			if (((Element) _o).element("Accout").equals(id) == true) {
				if (((Element) _o).element("Password").equals(pwd) == true) {
					return ((Element) _o).attributeValue("name");
				} else {
					return null;
				}
			}
		}
		return null;
	}
}
